package com.star.banner.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.star.banner.dto.BannerPageDTO;
import com.star.banner.dto.BannerSaveDTO;
import com.star.banner.dto.BannerUpdateDTO;
import com.star.banner.entity.Banner;
import com.star.banner.mapper.BannerMapper;
import com.star.banner.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.banner.struct.BannerStruct;
import com.star.banner.vo.BannerVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 横幅 服务实现类
 *
 * @author lty
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override
    public void save(BannerSaveDTO dto) {
        this.check(dto,null);
        Banner banner = BannerStruct.INSTANCE.to(dto).setId(null);
        this.save(banner);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void update(BannerUpdateDTO dto) {
        Long id = dto.getId();
        this.check(dto,id);
        this.update(BannerStruct.INSTANCE.to(dto), Wrappers.<Banner>lambdaUpdate()
                        .eq(Banner::getId,id)
                .set(Banner::getTitle,dto.getTitle())
                .set(Banner::getPositions,dto.getPositions())
                .set(Banner::getCoverUrl,dto.getCoverUrl())
                .set(Banner::getType,dto.getType())
                .set(Banner::getLinkUrl,dto.getLinkUrl())
                .set(Banner::getCustomContent,dto.getCustomContent())
        );

    }

    @Override
    public IPage<BannerVO> page(BannerPageDTO dto) {
        return this.getBaseMapper().page(dto);
    }

    @Override
    public Banner getDetail(Long id) {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getId,id);
        return this.getBaseMapper().selectOne(queryWrapper);
    }

    @Override
    public List<Banner> listBanner(String positions) {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(positions),Banner::getPositions,positions);
        return this.getBaseMapper().selectList(queryWrapper);
    }

    /**
     * 检查
     *
     * @param dto dto
     */
    private void check(BannerSaveDTO dto, Long id) {
        boolean save = id == null;
        String title = dto.getTitle();
        Banner banner = save ? null : Assert.notNull(this.getById(id), "横幅不存在");
        if (save || !Objects.equals(banner.getTitle(), title)) {
            Assert.isTrue(this.lambdaQuery().eq(Banner::getTitle, title).count() == 0
                    , "横幅标题已存在");
        }
    }
}
